Thread: [B]a basic doubt !![/B]

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    [B]a basic doubt !![/B]

    i just run the following code.


    Code:
    #include<stdio.h>
    main( )
    {
      int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
      printf("%u %u %u %d \n",a,*a,**a,***a);
    	  printf("%u %u %u %d \n",a+1,*a+1,**a+1,***a+1);
    	 getch();
    	 }
    and got the following result

    1245008 1245008 1245008 2
    1245032 1245016 1245012 3

    but i am not getting the concept behind the output,
    so anybody plz throw some light on it

    thanks!
    sam

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    the first 3 numbers of each line are memory addresses. the last number is a value stored in the array.

    i won't get into the details, but a[0] points to 1245008 or a[0][0], which points to 1245008 or a[0][0][0] or the value 2. the address of "a" is also 1245008.

    if you want to get down in there and study it, add the loop below
    Code:
    int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
    printf("%u %u %u %d \n",a,*a,**a,***a);
    printf("%u %u %u %d \n",a+1,*a+1,**a+1,***a+1);
    
    for(int i = 0; i < 2; i++)
        for(int j = 0; j < 3; j++)
          for(int k = 0; k < 2; k++)
          {
    	printf("The memory location %u holds the value %d (a[%d][%d][%d]) \n",
                              &a[i][j][k], a[i][j][k], i,j,k);     
          }
    Last edited by misplaced; 04-29-2005 at 05:07 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    thanks for replying
    but just tell me

    ***a+1=3?
    i mean which 3 and why??

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    disclaimer: technically, some of the following may be inaccurate. i was going for logic, which may also be inaccurate. however, it should definitly be accurate enough to get your mind going in the right direction

    basically, a multi-dimensional array is simply just an array.

    let's look at at one dimensional array. (let's pretend that an int is one byte, which it isn't)

    int a[10];

    this reserves 10 bytes of memory. "a" is really a pointer (again, some may argue, but for the sake of simplicity, let's not).
    it points to that first byte in memory that is reserved for the array.
    all the bytes are guarenteed to be in sequential order when the
    array is declared as such. when you ask for a[5], the compiler
    finds the address that "a" points to. then, it will move "up" 5
    bytes in memory and return the value stored there. a[5] is the
    same as *a+5. "a" is referred to as a segment. the five is referred to
    as the offset. in short, a[5] and *a+5 says give me the value that is stored in
    five bytes past "a".


    a two-dimensional pointer acts the same way, only, now
    it has to resolve not one, but two offsets. say we declare:

    int b[4][10];

    the compiler reserves 40 bytes in memory (4 *10), all in sequential order.
    b[0][0] is the first and b[4][10] is the last. when you ask for
    something like b[2][5], the compiler then "divides" the 40 bytes
    it reserved into 4 different sections, or sub-segments. it will then
    seek out the 3rd sub-segment (b[2][0], remember, we count 0,1,2,3). after it finds that sub-segment, it
    will then move up 5 more memory addresses and return the value located there.

    (the following is not tested)
    &b[2][5] should be equal to &b((4*10) / 4 * 2 + 5)

    multi dimensional arrays work on the same principle, only it will
    divide the array further into sub-sub-segments and sub-sub-sub-segments
    and so on.



    just so you don't get confused, i was pretending that and int was one byte for simplicity.
    in reality, it's not. when you say a+5 it REALLY means the address "a"
    points to plus (5 * sizeof(int)). but the compiler already does the sizeof(int) calculation
    so you don't have to.


    just to demonstrate a multidimensional array is simply just an array,
    add this code to yours
    Code:
     int *b = &a[0][0][0];
     for(int l = 0; l < 12; l++, b++)
             printf("%d\n", *b);

    as for your specific question
    ***a+1 = 3 would change the first 4 to a 3.

    more or less, each * used refers to another level of sub-segments.

    ***a+1
    * says a[0]
    ** says a[0][0]
    *** says a[0][0][0]
    ***a+1 says a[0][0][1]

    EDIT: next 3 lines changed
    with **a+1,
    the one adds one to the sub-segment, or refers to a[0][1][]
    similarily *a+1 refers to a[1][][].


    hope this helps
    Last edited by misplaced; 04-29-2005 at 01:07 PM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    ***a+1 = 3 would change the first 4 to a 3.
    Not quite. ***a+1 would grab the first index of the array and add 1 to it. ***a+1 isn't a valid lvalue.

    Consider the following:
    Code:
    itsme@dreams:~/C$ cat pntr.c
    #include <stdio.h>
    
    int main(void)
    {
      int array[2] = { 3, 86 };
    
      printf("%d %d\n", *array+1, *(array + 1));
      return 0;
    }
    itsme@dreams:~/C$ ./pntr
    4 86
    You would have to do ***(a+1) = 3 to change the first 4 to a 3. Ahh, the joys of precedence.
    Last edited by itsme86; 04-29-2005 at 11:23 AM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    my foux pox

    he just threw the ***a+1 = 3 out there and i bit on it...could have sworn i tested it though...oh well
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    thank u guys, u r great!!!1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function calling: basic doubt
    By doubty in forum C Programming
    Replies: 10
    Last Post: 06-23-2009, 02:31 AM
  2. basic doubt
    By vikranth in forum C Programming
    Replies: 7
    Last Post: 10-30-2007, 11:21 AM
  3. Basic Doubt in Virtual Classes
    By pritin in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 10:45 AM
  4. basic doubt in pointer concept
    By sanju in forum C Programming
    Replies: 1
    Last Post: 10-24-2002, 11:35 PM